package com.ffyc.backserver.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.ffyc.backserver.common.CommonResult;
import com.ffyc.backserver.common.JWTUtil;
import com.ffyc.backserver.model.Admin;
import com.ffyc.backserver.model.Menu;
import com.ffyc.backserver.model.Role;
import com.ffyc.backserver.service.AdminService;
import com.ffyc.backserver.util.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;


/*   *//*允许跨域访问*//*
    @CrossOrigin("http://localhost:8080")*/
@RestController

@RequestMapping(path = "/api/admin")

public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping(value = "findAdmin")
    /*
    管理员查询
     */
    public CommonResult findAdmin(@RequestBody Admin admin) {
        CommonResult commonResult = null;
        try {
            PageInfo<Admin> admins = adminService.findAdmin(admin);
            if (admins != null) {
                commonResult = new CommonResult<List<Admin>>(200, "查询成功", admins.getList(), admins.getTotal());
            } else {
                commonResult = new CommonResult<Menu>(201, "查询失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult<Menu>(500, "服务器错误", null);
        }

        return commonResult;
    }

    @RequestMapping(path = "roles")
    public CommonResult findRoles() {
        CommonResult commonResult = null;
        try {
            List<Role> roles = adminService.findRoles();
            if (roles != null) {
                commonResult = new CommonResult<List<Role>>(200, "查询成功", roles);
            } else {
                commonResult = new CommonResult<Role>(201, "查询失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult<Role>(500, "服务器错误", null);
        }

        return commonResult;
    }


    /**
     * 添加管理员
     *
     * @return
     */

    @RequestMapping(path = "save")
    public CommonResult saveAdmin(@RequestBody Admin admin) {
        CommonResult commonResult = null;
        try {
            adminService.saveAdmin(admin);
            commonResult = new CommonResult<Admin>(200, "管理员添加成功", admin);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult<Admin>(500, "服务器错误", null);
        }

        return commonResult;
    }


    @GetMapping(value = "findAdminById/{id}")
    /*
    根据id管理员查询
    restful 风格接口规范 get post put delete 用不同的请求方式表示不同的操作
     */
    public CommonResult findAdminById(@PathVariable("id") Integer id) {
        CommonResult commonResult = null;
        try {

            Admin admins = adminService.findAdminById(id);
            if (admins != null) {
                commonResult = new CommonResult<Admin>(200, "查询成功", admins);
            } else {
                commonResult = new CommonResult<Menu>(201, "查询失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult<Menu>(500, "服务器错误", null);
        }

        return commonResult;
    }


    @GetMapping(value = "deleteAdmin/{id}")
    /*
    根据id管理员查询
    restful 风格接口规范 get post put delete 用不同的请求方式表示不同的操作
     */
    public CommonResult deleteAdmin(@PathVariable("id") Integer id) {
        CommonResult commonResult = null;
        try {
            adminService.deleteAdmin(id);
            commonResult = new CommonResult<Admin>(200, "管理员删除成功", null);

        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult<Admin>(500, "服务器错误", null);
        }

        return commonResult;
    }


    @RequestMapping(path = "updataAdmin")
    /*
    根据id管理员查询
    restful 风格接口规范 get post put delete 用不同的请求方式表示不同的操作
     */
    public CommonResult updataAdmin(@RequestBody Admin admin) {
        CommonResult commonResult = null;
        try {
            //先删除
            adminService.updataAdmin(admin);
            commonResult = new CommonResult<Admin>(200, "管理员修改成功", admin);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult<Admin>(500, "服务器错误", null);
        }

        return commonResult;
    }


    /**
     * 头像上传
     *
     * @param
     * @return
     */
    @RequestMapping(path = "uploadImg")
    public CommonResult uploadImg(@RequestParam("fileName") CommonsMultipartFile file,@RequestHeader("token") String token) {
        CommonResult commonResult = null;
        /*从token中解析用户数据*/
        DecodedJWT tokenInfo= JWTUtil.getTokenInfo(token);
        Integer id=tokenInfo.getClaim("id").asInt();
        String account=tokenInfo.getClaim("account").asString();


        /*文件存贮位置*/
        File file1=new File("E:\\桌面文件夹地址\\安装包\\阿帕奇工具\\apache-tomcat-9.0.43\\webapps\\ROOT\\userImg\\"+account);
       /*如果这个文件夹不存在,就创建这个文件夹*/
        if (!file1.exists()){
            file1.mkdir();
        }
        /*得到上传时的文件名*/
        String oldFileName=file.getOriginalFilename();
        String newImgname= StringUtil.newFileName(oldFileName);
        File file2=new File(file1,newImgname);//输出路径
        try {
            /*将上传文件写到服务器指定的文件*/
            file.transferTo(file2);
            Admin admin=new Admin();
            admin.setId(id);
            admin.setNewImgname(newImgname);
            admin.setOldFileName(oldFileName);
            adminService.uploadImg(admin);
            String imgurl="http://127.0.0.1:8000/userImg/"+account+"/"+newImgname;
            commonResult = new CommonResult(200, "上传成功", imgurl);

        }catch (IOException e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "上传失败", null);

        }

        return commonResult;
    }

}